- content_for :javascript do
  ST.initializeManageMembers();

- content_for :title_header do
  %h1
    = t("layouts.admin.admin")
    = "-"
    = t("admin.communities.manage_members.manage_members")

= render :partial => "admin/left_hand_navigation", :locals => { :links => admin_links_for(@presenter.community) }

- content_for :ajax_update do
  #admin-members-saving-posting-allowed.ajax-update-notice
    %span.ajax-update-icon{:class => "#{icon_class("loading")} icon-fix" }
    %span.ajax-update-text
      = t("admin.communities.manage_members.saving_user_status")
  #admin-members-saved-posting-allowed.ajax-update-success
    %span.ajax-update-icon{:class => "#{icon_class("check")} icon-fix" }
    %span.ajax-update-text
      = t("admin.communities.manage_members.save_user_status_successful")
  #admin-members-error-posting-allowed.ajax-update-error
    = t("admin.communities.manage_members.save_user_status_error")

= render :partial => "layouts/ajax_update_notification"

.left-navi-section
  %h2= t("admin.communities.manage_members.manage_members", :community_name => @presenter.community.name(I18n.locale))

  = form_tag '', :method => :get do
    .row
      .col-6
        = text_field_tag :q, params[:q], placeholder: t('admin.communities.manage_members.search_by_name_email')
      .col-3.status-select-wrapper
        .status-select-button
          = @presenter.selected_statuses_title

        .status-select-dropdown
          .status-select-line.reset{data: {status: 'all'}}
            = @presenter.selected_statuses_title
          - @presenter.sorted_statuses.each do |status, status_translation, checked|
            .status-select-line{class: checked ? 'selected' : '', data: {status: status}}
              = check_box_tag "status[]", status, checked, id: "status-#{status}"
              = status_translation
      .col-3
        = submit_tag t("admin.communities.manage_members.search"), class: 'button search-button'
        = link_to    t("admin.communities.manage_members.reset_search"), admin_community_community_memberships_path(@presenter.community.id)

  - if params[:q].blank?
    #export-as-csv.right= link_to(" " + t("admin.communities.manage_members.export_all_as_csv"), {format: :csv}, {class: "js-users-csv-export " + icon_class("download")})

  %span#admin_members_count
    = AdminCommunityMembershipsViewUtils.community_members_entries_info(@presenter.community.id, @presenter.memberships).html_safe
    - if params[:q].present?
      = t("admin.communities.manage_members.for_search_terms", terms: "<strong>#{h params[:q]}</strong>".html_safe).html_safe

  %form
    %table#admin_members_list
      %thead
        %tr
          %th= render partial: "layouts/sort_link", locals: { column: "name", direction: sort_link_direction("name"), title: t("admin.communities.manage_members.name") }
          %th= render partial: "layouts/sort_link", locals: { column: "email", direction: sort_link_direction("email"), title: t("admin.communities.manage_members.email") }
          %th= render partial: "layouts/sort_link", locals: { column: "join_date", direction: sort_link_direction("join_date"), title: t("admin.communities.manage_members.join_date") }
          - if @presenter.require_verification_to_post_listings
            %th{:style => "text-align: center"}= render partial: "layouts/sort_link", locals: { column: "posting_allowed", direction: sort_link_direction("posting_allowed"), title: t("admin.communities.manage_members.posting_allowed") }
          %th= t("admin.communities.manage_members.admin")
          %th= t("admin.communities.manage_members.ban_user")
          %th
      %tbody
        - @presenter.memberships.each do |membership|
          - accepted_or_banned = membership.accepted? || membership.banned?
          - banned = membership.banned?
          - member = membership.person
          - unfinished_transactions = @presenter.has_membership_unfinished_transactions(membership)
          - unless member.blank?
            %tr{class: "member-#{membership.status}"}
              %td.admin-members-full-name
                = link_to @presenter.person_name(member), member
              %td
                - email = member.confirmed_notification_emails.any? ? member.confirmed_notification_email_addresses.first : member.emails.first.try(:address)
                = mail_to email
              %td
                - if accepted_or_banned
                  %span= l(membership.created_at, format: :short_date)
                - elsif membership.pending_consent?
                  = render 'pending_consent', membership: membership, member: member
                - elsif membership.pending_email_confirmation?
                  = render 'pending_confirmation', membership: membership, member: member
              - if @presenter.require_verification_to_post_listings
                %td{:style => "text-align: center"}
                  - if accepted_or_banned
                    = check_box_tag "posting-allowed[#{member.id}]", member.id, membership.can_post_listings, :class => "admin-members-can-post-listings"
              %td{:style => "text-align: center"}
                - if accepted_or_banned
                  = check_box_tag "is_admin[#{member.id}]", member.id, member.is_marketplace_admin?(@presenter.community), :class => "admin-members-is-admin", :disabled => member.eql?(@current_user) || member.banned?
              %td{:style => "text-align: center"}
                - if accepted_or_banned
                  = check_box_tag "is_banned[#{member.id}]", member.id, member.banned?,
                    data: {ban_url: ban_admin_community_community_membership_path(@presenter.community.id, membership.id),
                    unban_url: unban_admin_community_community_membership_path(@presenter.community.id, membership.id)},
                    class: "admin-members-ban-toggle"
              %td.membership-actions{data: {unfinished_transactions: unfinished_transactions}}
                - edit_class = banned ? 'is-disabled' : ''
                %span{class: edit_class}
                  = link_to person_settings_path(member) do
                    = icon_tag("edit")
                - post_klass = @presenter.can_post_listing(membership) ? '' : 'post-listing-is-disabled'
                %span.post-listing{class: "#{edit_class} #{post_klass}"}
                  = link_to new_person_listing_path(person_id: member.username) do
                    = icon_tag("plus")
                %span.delete-member{class: @presenter.can_delete(membership) ? '' : 'is-disabled',
                  title: @presenter.delete_member_title(membership)}
                  = link_to admin_community_community_membership_path(id: membership.id) do
                    = icon_tag("trash")
  .row
    .col-12
      = will_paginate @presenter.memberships

= render layout: "layouts/lightbox", locals: { id: "membership-popup"} do
  #membership-popup-content.membership-popup-content

= render layout: "layouts/lightbox", locals: { id: "membership-delete"} do
  #membership-delete-content.membership-popup-content.delete-content
    .membership--pending-popup
      %p=t('admin.communities.manage_members.delete_info1')
      %p=t('admin.communities.manage_members.delete_info2')
    .membership--pending-buttons
      = link_to '#', id: 'delete-membership-link',
        class: 'button membership-resend-confirmation', data: {method: "delete"} do
        = t('admin.communities.manage_members.confirm')
      = link_to '#', class: 'cancel-button', id: 'close_x1' do
        = t('admin.communities.manage_members.cancel')

- js_t(['admin.communities.manage_members.ban_user_confirmation',
  'admin.communities.manage_members.unban_user_confirmation',
  'admin.communities.manage_members.this_makes_the_user_an_admin',
  'admin.communities.manage_members.status_filter.all',
  'admin.communities.manage_members.status_filter.selected_js',
  'admin.communities.manage_members.have_ongoing_transactions',
  'admin.communities.manage_members.only_delete_disabled'])

